package com.yonyou.cyxdms.sysAuth.service.service.impl;

import com.alibaba.fastjson.JSONArray;
import com.yonyou.cyx.framework.compent.redis.RedisClient;
import com.yonyou.cyx.function.exception.ServiceBizException;
import com.yonyou.cyxdms.framework.bean.dto.user_info.LoginInfoDto;
import com.yonyou.cyxdms.sysAuth.service.constants.UserAuthConstant;
import com.yonyou.cyxdms.sysAuth.service.service.UserAuthService;
import com.yonyou.cyxdms.sysAuth.service.service.UserService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * 定义GMMC 的用户认证信息
 *
 * @author: zhangxianchao
 * @since: 2018/9/22 0022
 * @updateDate:
 * @updateRemark:
 * @version:1.0 Copyright: Copyright (c) 2018
 */
@Service(UserAuthConstant.SERVICE_NAME_USER_AUTH + UserAuthConstant.APP_SOURCE_HELPDESK)
@Transactional(rollbackFor = Exception.class)
public class HelpDeskUserAuthServiceImpl implements UserAuthService {

    // 日志对象
    private static final Logger logger = LoggerFactory.getLogger(HelpDeskUserAuthServiceImpl.class);
    @Autowired
    public UserService userService;
    @Autowired
    private RedisClient redisClient;
    @Autowired
    private RedisTemplate redisTemplate;

    /**
     * 根据主应用的用户信息获取自己应用的用户信息
     *
     * @param token 主应用的用户信息
     * @return org.springframework.http.ResponseEntity
     * @author zhangxianchao
     * @since 2018/9/22 0022
     */
    @Override
    @Transactional(readOnly = true)
    public LoginInfoDto getAppUserInfo(String token) {
        logger.debug("token:{}", token);
        if (redisClient.exists(token)) {
            Object jwtInfo = redisTemplate.opsForValue().get(token);
            if (jwtInfo != null) {
                // 取得登录账号
                logger.info("jwtInfo:{}", com.yonyou.cyx.function.utils.jsonserializer.JSONUtil.objectToJson(jwtInfo));
                JSONArray jwtInfoJson = (JSONArray) jwtInfo;
                logger.info("jwtInfoObject:{}", com.yonyou.cyx.function.utils.jsonserializer.JSONUtil
                        .objectToJson(jwtInfoJson.getJSONObject(1)));
                logger.info("jwtInfoObject:{}", com.yonyou.cyx.function.utils.jsonserializer.JSONUtil
                        .objectToJson(jwtInfoJson.getJSONObject(1).getJSONArray("param")));
                JSONArray jwtParam = jwtInfoJson.getJSONObject(1).getJSONArray("param");
                Long userId = jwtParam.getJSONObject(1).getLong("userId");
                LoginInfoDto result = userService.getUserInfoByUserId(UserAuthConstant.APP_SOURCE_HELPDESK, userId);
                if (result == null) {
                    throw new ServiceBizException("用户不存在, appId@userCode:{}",
                            UserAuthConstant.APP_SOURCE_HELPDESK + "@" + userId);
                }
                return result;
            } else {
                throw new ServiceBizException("--jwt已失效");
            }
        } else {
            throw new ServiceBizException("jwt不存在！");
        }
    }
}
